﻿using System;
using Csla.Xaml;
using Accounting.Library;
using System.ComponentModel;
using Utility;
using Accounting.Library.Utility;


namespace csla4Skeleton.UI.SL.NghiepVu.BanSanPham.BSP_BanHangHoa
{
    public class BSP_BanHangHoa_ViewModel : ViewModel<Accounting.Library.BSP_BanHangHoa>
    {
        #region Event Declare
        public delegate void ChildList_AddedNew_EventHandler(object sender, ChildListAddedNewArgs e);
        public delegate void DKNV_EventHandler(object sender, DieuKienNghiepVuEventArgs e);
        public delegate void HangHoaIdChanged_EventHandler(object sender, HangHoaIdChangedEventArgs e);
        public delegate void TKCoIdChanged_EventHandler(object sender, TKCoIdChangedEventArgs e);
        public delegate void ThueSuatIdChanged_EventHandler(object sender, ThueSuatIdChangedEventArgs e);
        public delegate void SoLuongChanged_EventHandler(object sender, SoLuongChangedEventArgs e);
       
        public event ChildList_AddedNew_EventHandler ChildList_AddedNew;

        public event DKNV_EventHandler stateChanged;
        public event EventHandler Saved;
       
        public event EventHandler NgayChungTuChanged;
        public event EventHandler TCHangHoaChanged;
        public event EventHandler LoaiTienTeChanged;
        public event EventHandler LoadCompleted;
        public event EventHandler TongTienThanhToanChanged;
        public event EventHandler TongTienHangChanged;
        public event EventHandler TienThueGTGTChanged;
        public event EventHandler EnableButtonLapPhieu;
        public event HangHoaIdChanged_EventHandler hangHoaIdChanged;
        public event TKCoIdChanged_EventHandler TKCoIdChanged;
        public event ThueSuatIdChanged_EventHandler ThueSuatIdChanged;
        public event SoLuongChanged_EventHandler SoLuongChanged;  
        #endregion

        #region Refresh Model
        public BSP_BanHangHoa_ViewModel()
        {
            BeginRefresh("NewBSP_BanHangHoa");
            
        }

        public BSP_BanHangHoa_ViewModel(int Khoa)
        {
            BeginRefresh("GetBSP_BanHangHoa", Khoa);
           
        }

        public void NewBSP_BanHangHoaViewModel()
        {
            BeginRefresh("NewBSP_BanHangHoa");
            
        }

        public void GetBSP_BanHangHoaViewModel(int Khoa)
        {
            BeginRefresh("GetBSP_BanHangHoa", Khoa);
        }

       
        public void SaveAndNew()
        {
            BeginSave();
            NewBSP_BanHangHoaViewModel();
        }
        public void SaveBanHangHoa()
        {
            BeginSave();

        }
        protected override void OnSaved()
        {
            base.OnSaved();
            //OnRefreshed();
            if (EnableButtonLapPhieu != null)
            {
                EnableButtonLapPhieu(this, new EventArgs());
            }
            if (Saved != null)
            {
                Saved(this, new EventArgs());
            }
        }
        protected override void OnRefreshed()
        {
            base.OnRefreshed();
            if (Model != null)
            {
                Model.BSP_BanHangHoa_ChiTiets.AddedNew += new EventHandler<Csla.Core.AddedNewEventArgs<BSP_BanHangHoa_ChiTietChild>>(BSP_BanHangHoa_ChiTiets_AddedNew);
                Model.BSP_BanHangHoa_Thues.AddedNew += new EventHandler<Csla.Core.AddedNewEventArgs<BSP_BanHangHoa_ThueChild>>(BSP_BanHangHoa_Thues_AddedNew);
                Model.BSP_BanHangHoa_PhuPhis.AddedNew += new EventHandler<Csla.Core.AddedNewEventArgs<BSP_BanHangHoa_PhuPhiChild>>(BSP_BanHangHoa_PhuPhis_AddedNew);
                Model.BSP_BanHangHoa_GiaVons.AddedNew += new EventHandler<Csla.Core.AddedNewEventArgs<BSP_BanHangHoa_GiaVonChild>>(BSP_BanHangHoa_GiaVons_AddedNew);
                
                Model.NhomDKNVChanged += new EventHandler < DieuKienNghiepVuEventArgs>(OnStateChanged);
                Model.TinhChatHangHoaChanged += new EventHandler(OnTinhChatHangHoaChanged);
                Model.HangHoaIdChanged += new EventHandler<HangHoaIdChangedEventArgs>(OnHangHoaIdChanged);
                Model.TKCoChanged += new EventHandler<TKCoIdChangedEventArgs>(OnTKCoChanged);
                Model.SoLuongChanged += new EventHandler<SoLuongChangedEventArgs>(OnSoLuongChanged);
                Model.LoaiTienTeChanged+= new EventHandler(OnLoaiTienTeChanged);
                Model.EnableButtonLapPhieu += new EventHandler(OnEnableButtonLapPhieu);
                Model.ThueSuatIdChanged += new EventHandler<ThueSuatIdChangedEventArgs>(OnThueSuatIdChanged);
                Model.TongTienThanhToanChanged += new EventHandler(OnTongTienThanhToanChanged);
                Model.TongTienHangChanged += new EventHandler(OnTongTienHangChanged);
                Model.TienThueGTGTChanged += new EventHandler(OnTienThueGTGTChanged);
                Model.NgayChungTuChanged += new EventHandler(OnNgayChungTuChanged);
               
               
                Model.BusyEvent += new EventHandler<IsBusyEventArgs>(DoChangingBusy);

                if (LoadCompleted != null)
                {
                    LoadCompleted(this, new EventArgs());
                }
            }
        }
        #endregion
        
        #region Add BSP_BanHangHoa Details Item
        public void AddBSP_BanHangHoa_ChiTietItem()
        {
            Model.BSP_BanHangHoa_ChiTiets.AddNewItem(Model.Khoa);
        }

        public void AddBSP_BanHangHoa_ThueItem()
        {
            Model.BSP_BanHangHoa_Thues.AddNewItem(Model.Khoa);
        }

        public void AddBSP_BanHangHoa_PhuPhiItem()
        {
            Model.BSP_BanHangHoa_PhuPhis.AddNewItem(Model.Khoa);
        }

        public void AddBSP_BanHangHoa_GiaVonItem()
        {
            Model.BSP_BanHangHoa_GiaVons.AddNewItem(Model.Khoa);
        }

       



        void BSP_BanHangHoa_ChiTiets_AddedNew(object sender, Csla.Core.AddedNewEventArgs<BSP_BanHangHoa_ChiTietChild> e)
        {
            OnChildList_AddedNew(sender, new ChildListAddedNewArgs("BSP_BanHangHoa_ChiTiets"));
        }      

        void BSP_BanHangHoa_Thues_AddedNew(object sender, Csla.Core.AddedNewEventArgs<BSP_BanHangHoa_ThueChild> e)
        {
            OnChildList_AddedNew(sender, new ChildListAddedNewArgs("BSP_BanHangHoa_Thues"));
        }

        void BSP_BanHangHoa_PhuPhis_AddedNew(object sender, Csla.Core.AddedNewEventArgs<BSP_BanHangHoa_PhuPhiChild> e)
        {
            OnChildList_AddedNew(sender, new ChildListAddedNewArgs("BSP_BanHangHoa_PhuPhis"));
        }

        void BSP_BanHangHoa_GiaVons_AddedNew(object sender, Csla.Core.AddedNewEventArgs<BSP_BanHangHoa_GiaVonChild> e)
        {
            OnChildList_AddedNew(sender, new ChildListAddedNewArgs("BSP_BanHangHoa_GiaVons"));
        }


        public void OnTongTienThanhToanChanged(object sender, EventArgs e)
        {

            if (TongTienThanhToanChanged != null)
            {
                TongTienThanhToanChanged(this, new EventArgs());
            }

        }
        public void OnTongTienHangChanged(object sender, EventArgs e)
        {

            if (TongTienHangChanged != null)
            {
                TongTienHangChanged(this, new EventArgs());
            }

        }
        public void OnTienThueGTGTChanged(object sender, EventArgs e)
        {

            if (TienThueGTGTChanged != null)
            {
                TienThueGTGTChanged(this, new EventArgs());
            }

        }

        public void OnNgayChungTuChanged(object sender, EventArgs e)
        {

            if (NgayChungTuChanged != null)
            {
                NgayChungTuChanged(this, new EventArgs());
            }

        }
       
        protected virtual void OnChildList_AddedNew(object sender, ChildListAddedNewArgs e)
        {
            if (ChildList_AddedNew != null)
            {
                //Invokes the delegates.
                ChildList_AddedNew(sender, e);
            }
        }

        public void OnStateChanged(object sender, DieuKienNghiepVuEventArgs e)
        {

            if (stateChanged != null)
            {
                stateChanged(sender, e);
            }
          
        }
        public void OnSoLuongChanged(object sender, SoLuongChangedEventArgs e)
        {
            if (SoLuongChanged != null)
            {
                SoLuongChanged(sender, e);
            }

        }
        public void OnHangHoaIdChanged(object sender, HangHoaIdChangedEventArgs e)
        {
            if (hangHoaIdChanged != null)
            {
                hangHoaIdChanged(sender, e);
            }

        }
        public void OnTinhChatHangHoaChanged(object sender, EventArgs e)
        {
            if (TCHangHoaChanged != null)
            {
                TCHangHoaChanged(sender, e);
            }

        }
        public void OnLoaiTienTeChanged(object sender, EventArgs e)
        {
            if (TCHangHoaChanged != null)
            {
                LoaiTienTeChanged(sender, e);
            }
        }

        public void DoChangingBusy(bool busy)
        {
            IsBusy = busy;
        }
        public void DoChangingBusy(object sender, IsBusyEventArgs e)
        {
            IsBusy = e.Busy;
        }

        public void OnTKCoChanged(object sender,TKCoIdChangedEventArgs  e)
        {
            if (TKCoIdChanged != null)
            {
                TKCoIdChanged(sender, e);
            }

        }

        public void OnThueSuatIdChanged(object sender, ThueSuatIdChangedEventArgs e)
        {
            if (ThueSuatIdChanged != null)
            {
                ThueSuatIdChanged(sender, e);
            }

        }

        public void OnEnableButtonLapPhieu(object sender, EventArgs e)
        {
            if (EnableButtonLapPhieu != null)
            {
                EnableButtonLapPhieu(sender, e);
            }
        }

        #endregion

        #region Delete BSP_BanHangHoa Details Item

        public void DeleteBSP_BanHangHoa_ChiTietItem(int Index)
        {
            Model.BSP_BanHangHoa_ChiTiets.DeleteItem(Index);
        }

        public void DeleteBSP_BanHangHoa_ThueItem(int Index)
        {
            Model.BSP_BanHangHoa_Thues.DeleteItem(Index);
        }
        public void DeleteBSP_BanHangHoa_PhuPhiItem(int Index)
        {
            Model.BSP_BanHangHoa_PhuPhis.DeleteItem(Index);
        }

        public void DeleteBSP_BanHangHoa_GiaVonItem(int Index)
        {
            Model.BSP_BanHangHoa_GiaVons.DeleteItem(Index);
        }

        

        #endregion

        #region Nghiep Vu So
        internal void GhiSo()
        {
            this.Model.SaveMode = BusinessConfiguration.Configuration.GhiSoSaveMode;
            BeginSave();
        }

        internal void BoGhi()
        {
            int loaiChungTuId = BusinessConfiguration.Configuration.LoaiChungTuId_BSP_BanHangHoa;
            int chungTuGocId = Model.Khoa;

            this.IsBusy = true;
            Accounting.Library.GhiSo.BoGhiService.BoGhi(loaiChungTuId, chungTuGocId, (o, ex) =>
            {
                if (ex.Error != null)
                    this.Model.ErrorMessage = ex.Error.Message;
                else
                    this.Model.ErrorMessage = ((Accounting.Library.GhiSo.BoGhiService)ex.Object).ErrorMessage;
                BeginRefresh("GetBSP_BanHangHoa", Model.Khoa);
            });
            this.IsBusy = false;
        }
#endregion
    }
}
